﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Data;
using System.Collections;

namespace StockMarketMainEngine
{
    class Program
    {
        private static DataBase db;
        public static int elapsedTime;
        private static DataTable table;

        static void Main(string[] args)
        {

            db = new DataBase();
            DataSet ds = db.GetNumberOfGames();
            table = ds.Tables["games"];

            //foreach (DataRow row in table.Rows) // Loop over the rows.
            //{
                //elapsedTime = Convert.ToInt32(row["GameElapsedSpeed"].ToString());
                Timer timer = new Timer(TimerTick, null, TimeSpan.Zero, new TimeSpan(0, 0, 0, 1));
            //TimerTick(null);
            //}

            Console.Read();
        }

        public static void TimerTick(object state)
        {
            int lastSecond = 1;
            DateTime time = DateTime.Now;
            int second =time.Second;
            //int second = 20;
            int change = 1;


            foreach (DataRow row in table.Rows) // Loop over the rows.
            {
                elapsedTime = Convert.ToInt32(row["GameElapsedSpeed"].ToString());

                if ((second % elapsedTime == 0))
                {
                    if (change == 1)
                    {
                        Console.WriteLine("");
                        Console.WriteLine("**********************************************"); // Print separator.
                        Console.WriteLine("Server Time:" + time.ToLongTimeString());
                        Console.WriteLine("");
                        Console.WriteLine("");
                        change++;
                    }

                    lastSecond = second;
                    //Console.WriteLine(elapsedTime + ": " + row["GameID"].ToString());
                    update(Convert.ToInt32(row["GameID"].ToString()));
                }
              
            }

            if (change > 1)
            {
                DataSet ds = db.GetGameStatus();
                DataTable table2 = ds.Tables["gamestatus"];

                foreach (DataRow row in table2.Rows) // Loop over the rows.
                {
                    Console.WriteLine("---------------------------------------------"); // Print separator.
                    Console.WriteLine("Game Name: " + row["GameName"]);
                    Console.WriteLine("Game Current Date: " + row["GameElapsedDate"]);
                    Console.WriteLine("Game Speed (seconds) : " + row["GameElapsedSpeed"]);
                }
            }

            /*
            int second = DateTime.Now.Second;
            if (second != lastSecond && second % elapsedTime == 0)
            {
                lastSecond = second;
                Console.WriteLine("");
                Console.WriteLine("**********************************************"); // Print separator.
                Console.WriteLine("Server Time:" + DateTime.Now.ToLongTimeString());
                Console.WriteLine("");
                Console.WriteLine("");

                update();
            }
             * */
        }

        public static void update(int gameId)
        {
            db.UpdateShareSimulation(gameId);
        }
    }

}
